package com.earnings.okhttputils.utils.car.ui;

import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.callback.StringCallback;
import com.earnings.okhttputils.utils.UrlConstant.CarUrl;
import com.earnings.okhttputils.utils.bean.CarScreenBean;
import com.earnings.okhttputils.utils.bean.CarScreenBean1;
import com.earnings.okhttputils.utils.car.http.CarHttp;
import com.earnings.okhttputils.utils.car.http.GodHttpArrayCallback;
import com.earnings.okhttputils.utils.god.GodAdapter.GodArrayList;
import com.earnings.okhttputils.utils.god.GodAdapter.GodBaseAdapter;
import com.earnings.okhttputils.utils.god.GodAdapter.GodViewHolder;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.god.Listener.GodOnClickListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by Administrator on 2017/6/16 0016.
 */

public class ConditionCarActivity extends GodLeftHandBaseActivity implements GodOnClickListener {
    private GodBaseAdapter adapter1;
    private GodBaseAdapter adapter2;
    private GodBaseAdapter adapter3;
    private GodBaseAdapter adapter4;
    private GodArrayList<CarScreenBean1> arrayList1 = new GodArrayList<>();
    private GodArrayList<CarScreenBean1> arrayList2 = new GodArrayList<>();
    private GodArrayList<CarScreenBean1> arrayList3 = new GodArrayList<>();
    private GodArrayList<CarScreenBean1> arrayList4 = new GodArrayList<>();

    private String price = "";
    private String level = "";
    private String imports = "";
    private String stage = "";


    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_conditioncar;
    }

    @Override
    protected void init() {
        setTitle("条件找车");
        initRc1();
        initRc2();
        initRc3();
        initRc4();
        setOnClickListener(this, R.id.number_tv);
        CarHttp carHttp = new CarHttp("condition");
        http(carHttp);
    }

    private void initRc4() {
        adapter4 = new GodBaseAdapter<CarScreenBean1>(R.layout.item_condition, arrayList4) {
            @Override
            public void onBind(GodViewHolder holder, int position, CarScreenBean1 data) {
                holder.setText(R.id.name_tv, data.getName());
                if (getOptindex() == position) {
                    holder.setTextColor(R.id.name_tv, Color.WHITE);
                    holder.setBackgroundResource(R.id.name_tv, R.drawable.item_condition_bg2);
                } else {
                    holder.setTextColor(R.id.name_tv, Color.BLACK);
                    holder.setBackgroundResource(R.id.name_tv, R.drawable.item_condition_bg1);
                }
            }

            @Override
            public void OnItemClickListener(View view, int position, CarScreenBean1 data) {
                stage = data.getId();
                httpnumber();
            }
        };
        adapter4.setOneChoice(true);
        adapter4.setChoiceDrawableBackground(R.drawable.item_condition_bg1, R.drawable.item_condition_bg2);
        adapter4.setDefaultposition(0);
        setRecyclerViewLayoutManager(R.id.recyclerview4, new GridLayoutManager(this, 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        }).setAdapter(adapter4);
    }

    private void initRc3() {
        adapter3 = new GodBaseAdapter<CarScreenBean1>(R.layout.item_condition, arrayList3) {
            @Override
            public void onBind(GodViewHolder holder, int position, CarScreenBean1 data) {
                holder.setText(R.id.name_tv, data.getName());
                if (getOptindex() == position) {
                    holder.setTextColor(R.id.name_tv, Color.WHITE);
                    holder.setBackgroundResource(R.id.name_tv, R.drawable.item_condition_bg2);
                } else {
                    holder.setTextColor(R.id.name_tv, Color.BLACK);
                    holder.setBackgroundResource(R.id.name_tv, R.drawable.item_condition_bg1);
                }
            }

            @Override
            public void OnItemClickListener(View view, int position, CarScreenBean1 data) {
                imports = data.getId();
                httpnumber();
            }
        };
        adapter3.setOneChoice(true);
        adapter3.setChoiceDrawableBackground(R.drawable.item_condition_bg1, R.drawable.item_condition_bg2);
        adapter3.setDefaultposition(0);
        setRecyclerViewLayoutManager(R.id.recyclerview3, new GridLayoutManager(this, 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        }).setAdapter(adapter3);
    }

    private void initRc2() {
        adapter2 = new GodBaseAdapter<CarScreenBean1>(R.layout.item_condition, arrayList2) {
            @Override
            public void onBind(GodViewHolder holder, int position, CarScreenBean1 data) {
                holder.setText(R.id.name_tv, data.getName());
                if (getOptindex() == position) {
                    holder.setTextColor(R.id.name_tv, Color.WHITE);
                    holder.setBackgroundResource(R.id.name_tv, R.drawable.item_condition_bg2);
                } else {
                    holder.setTextColor(R.id.name_tv, Color.BLACK);
                    holder.setBackgroundResource(R.id.name_tv, R.drawable.item_condition_bg1);
                }
            }

            @Override
            public void OnItemClickListener(View view, int position, CarScreenBean1 data) {
                level = data.getId();
                httpnumber();
            }
        };
        adapter2.setOneChoice(true);
        adapter2.setChoiceDrawableBackground(R.drawable.item_condition_bg1, R.drawable.item_condition_bg2);
        adapter2.setDefaultposition(0);
        setRecyclerViewLayoutManager(R.id.recyclerview2, new GridLayoutManager(this, 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        }).setAdapter(adapter2);
    }

    private void initRc1() {
        adapter1 = new GodBaseAdapter<CarScreenBean1>(R.layout.item_condition, arrayList1) {
            @Override
            public void onBind(GodViewHolder holder, int position, CarScreenBean1 data) {
                holder.setText(R.id.name_tv, data.getName());
                if (getOptindex() == position) {
                    holder.setTextColor(R.id.name_tv, Color.WHITE);
                    holder.setBackgroundResource(R.id.name_tv, R.drawable.item_condition_bg2);
                } else {
                    holder.setTextColor(R.id.name_tv, Color.BLACK);
                    holder.setBackgroundResource(R.id.name_tv, R.drawable.item_condition_bg1);
                }
            }

            @Override
            public void OnItemClickListener(View view, int position, CarScreenBean1 data) {
                price = data.getId();
                httpnumber();
            }
        };
        adapter1.setOneChoice(true);
        adapter1.setChoiceDrawableBackground(R.drawable.item_condition_bg1, R.drawable.item_condition_bg2);
        adapter1.setDefaultposition(0);
        setRecyclerViewLayoutManager(R.id.recyclerview1, new GridLayoutManager(this, 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        }).setAdapter(adapter1);
    }

    private void http(CarHttp carHttp) {
        showProgress();
        OkHttpUtils.get().url(CarUrl.CAR_URL).params(carHttp).build().execute(new GodHttpArrayCallback<CarScreenBean>() {
            @Override
            public void onSuccess(List<CarScreenBean> datas, String msg, String response) {
                arrayList1.addAll(datas.get(0).getItems());
                for (int i = 0; i < arrayList1.getOneChoice().size(); i++) {
                    arrayList1.getOneChoice().get(i).setOpt(false);
                }
                arrayList1.getOneChoice().get(0).setOpt(true);
                arrayList1.notifyDataSetChanged();

                arrayList2.addAll(datas.get(1).getItems());
                for (int i = 0; i < arrayList2.getOneChoice().size(); i++) {
                    arrayList2.getOneChoice().get(i).setOpt(false);
                }
                arrayList2.getOneChoice().get(0).setOpt(true);
                arrayList2.notifyDataSetChanged();

                arrayList3.addAll(datas.get(2).getItems());
                for (int i = 0; i < arrayList3.getOneChoice().size(); i++) {
                    arrayList3.getOneChoice().get(i).setOpt(false);
                }
                arrayList3.getOneChoice().get(0).setOpt(true);
                arrayList3.notifyDataSetChanged();

                arrayList4.addAll(datas.get(3).getItems());
                for (int i = 0; i < arrayList4.getOneChoice().size(); i++) {
                    arrayList4.getOneChoice().get(i).setOpt(false);
                }
                arrayList4.getOneChoice().get(0).setOpt(true);
                arrayList4.notifyDataSetChanged();

                price = datas.get(0).getItems().get(0).getId();
                level = datas.get(1).getItems().get(0).getId();
                imports = datas.get(2).getItems().get(0).getId();
                stage = datas.get(3).getItems().get(0).getId();
                httpnumber();
                dismissProgress();
            }

            @Override
            public void onFail(int code, String msg) {
                dismissProgress();
            }
        });
    }

    private void httpnumber() {
        showProgress();
        Map map = new HashMap();
//        level	int	N	0	级别
//import	int	N	0	国别
//        price	string	N	0	价格区间
//        stage	int	N	0	货款
        map.put("level", level);
        map.put("import", imports);
        map.put("price", price);
        map.put("stage", stage);
        CarHttp carHttp = new CarHttp("car", map);
        OkHttpUtils.get().url(CarUrl.CAR_URL).params(carHttp).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                dismissProgress();
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsb = new JSONObject(response);
                    if (jsb.getString("status").equals("1")) {
                        setText(R.id.number_tv, "共找到" + jsb.getString("totalpages") + "个车型");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }


                dismissProgress();
            }
        });


    }

    @Override
    public void OnClickListener(View view) {
        mIntent.putExtra("level", level);
        mIntent.putExtra("import", imports);
        mIntent.putExtra("price", price);
        mIntent.putExtra("stage", stage);
        mIntent.putExtra("title", "符合条件的车辆");
        skipActivity(CarListActivity.class);
    }
}
